package com.zahb.qadx.ui.fragment;

import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.base.BaseFragmentExt;
import com.zahb.qadx.model.CommonResponse;
import com.zahb.qadx.model.RecommendModel;
import com.zahb.qadx.model.TeacherModel;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.adapter.RecommendAdapter;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by： huwei.
 * Data： 2020-10-18.
 * Time： 22:56.
 * Desc：推荐课程
 */
public class RecommendedCourseFragment extends BaseFragmentExt {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.ll_empty)
    LinearLayoutCompat llEmpty;
    private String relationId;
    private String ids;
    private Gson gson = new Gson();
    private List<RecommendModel.RecomendData.RecommendList> recommendLists = new ArrayList<>();
    private RecommendAdapter recommendAdapter;

    @Override
    protected int getLayout() {
        return R.layout.fragment_recommended;
    }

    @Override
    protected void initViews(View rootView) {
        mUnbinder = ButterKnife.bind(this, rootView);
        if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
            showProgressDialog("");
            addDisposable(RetrofitService.getNetService()
                    .getTeacher(relationId, 1)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        hideProgressDialog();
                        getDetailData(response);
                    }, throwable -> {
                        hideProgressDialog();
                        throwable.printStackTrace();
                        Tips.RequestError(getActivity());
                    }));
        }
    }

    private void getDetailData(TeacherModel teacherModel) {
        if (teacherModel.getStatusCode() == 200) {
            List<String> courseIds = teacherModel.getData().getCourseIds();
            getInfoTeacher(courseIds);
        } else {
            showBindToast(teacherModel.getErrorInfo());
        }
    }

    private void getInfoTeacher(List<String> courseIds) {
        if (courseIds==null||courseIds.size()==0){
            isVisible(false);
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("courseIds", courseIds);
        params.put("startPage", 1);
        params.put("pageSize", 15);
        String json = gson.toJson(params);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
            showProgressDialog("");
            addDisposable(RetrofitService.getNetService()
                    .getCourseInfoByTeacher(body)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        hideProgressDialog();
                        getCourseInfoByTeacherData(response);
                    }, throwable -> {
                        hideProgressDialog();
                        throwable.printStackTrace();
                        Tips.RequestError(getActivity());
                    }));
        }
    }

    private void getCourseInfoByTeacherData(CommonResponse<RecommendModel.RecomendData> response) {
        if (response.getStatusCode() == 200) {
            isVisible(true);
            recommendLists = response.getData().getList();
            LinearLayoutManager managerGrid = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(managerGrid);
            recommendAdapter = new RecommendAdapter(getActivity(), recommendLists);
            recyclerView.setAdapter(recommendAdapter);
        } else {
            isVisible(false);
        }
    }

    public void setParamse(String relationId) {
        this.relationId = relationId;
    }

    private void isVisible(boolean flag) {
        if (flag){
            recyclerView.setVisibility(View.VISIBLE);
            llEmpty.setVisibility(View.GONE);
        }else{
            llEmpty.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }
    }
}
